<%--
  Created by IntelliJ IDEA.
  User: pc
  Date: 2022/11/29
  Time: 17:31
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <base href="<%=request.getContextPath()+"/"%>">
    <title>Title</title>

    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="bootstrap-3.4.1/dist/css/bootstrap.cs">


    <script type="text/javascript" src="js/jquery.js"></script>

    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="bootstrap-3.4.1/dist/js/bootstrap.js"></script>

    <script type="text/javascript">
        $(function () {
            //给表单绑定一个点击事件
            $("#user-update-form").submit(function () {
                console.log("用户点击了表单提交")

                //1、获取表单数据
                var formData = $("#user-update-form").serializeArray();
                var param = new Object();

                for (var i = 0; i<formData.length; i++){
                    var name = formData[i].name;
                    var value = formData[i].value;
                    // [name]作为对象属性名称，value作为属性值
                    param[name] = value;// param.name="admin",param.age="age"
                }
                //2、转成JSON字符串
                var json = JSON.stringify(param);

                //3、发ajax请求
                $.ajax({
                    url: "http://localhost:8080/DeptServlet/updateDeptAsync",
                    type: "POST",
                    data: json,
                    contentType: "application/json",
                    dataType: "json",
                    success: function (data) {
                        if (data.code == 200) {
                            location.href = "http://localhost:8080/DeptServlet/alist"
                        }
                    }
                });
                // 这里返回false表单不会自动提交了
                return false;
            });
        })

    </script>

</head>
<body>

<form class="form-horizontal" id="user-update-form" method="post">
    <table border="1">
        <div class="form-group">
            <label for="dname" class="col-sm-2 control-label">部门</label>
            <div class="col-sm-2">
                <input type="hidden" id="deptno" name="deptno" value="${dept.deptno}">
                <input type="text" class="form-control" id="dname" name="dname" value="${dept.dname}">
            </div>
        </div>

        <div class="form-group">
            <label for="loc" class="col-sm-2 control-label">职位</label>
            <div class="col-sm-2">
                <input type="text" class="form-control" id="loc" name="loc" value="${dept.loc}">
            </div>
        </div>

        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="submit" class="btn btn-default">添加</button>
            </div>
        </div>

    </table>

</form>

</body>
</html>
